/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pantallas;

import clases.GestorGrabarLeerDelDisco;
import clases.Operacion;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author DanielRH
 */
public class PantallaOperaciones extends javax.swing.JFrame {

    /**
     * Creates new form PantallaOperaciones
     */
    public PantallaOperaciones() {
        initComponents();
        cargarTabla();
        cargarMouseListener();
    }
    
    public void setPadre(PantallaAgregarTrabajo pantPadre)
    {
        pantallaPadre=pantPadre;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnNueva = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaOperaciones = new javax.swing.JTable();
        btnAceptar = new javax.swing.JButton();
        txtNuevaOperacion = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtSeleccionada = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        btnNueva.setText("Agregar");
        btnNueva.setEnabled(false);
        btnNueva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevaActionPerformed(evt);
            }
        });

        tablaOperaciones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tablaOperaciones);

        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        txtNuevaOperacion.setEnabled(false);

        jLabel1.setText("Nueva Operacion:");

        jLabel2.setText("Seleccionada:");

        txtSeleccionada.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtSeleccionada)
                        .addGap(18, 18, 18)
                        .addComponent(btnAceptar))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNuevaOperacion)
                        .addGap(18, 18, 18)
                        .addComponent(btnNueva))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNueva)
                    .addComponent(txtNuevaOperacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAceptar)
                    .addComponent(jLabel2)
                    .addComponent(txtSeleccionada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNuevaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnNuevaActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        // TODO add your handling code here:
        pantallaPadre.setOperacionSeleccionada(txtSeleccionada.getText());
        this.dispose();
    }//GEN-LAST:event_btnAceptarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PantallaOperaciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PantallaOperaciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PantallaOperaciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PantallaOperaciones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PantallaOperaciones().setVisible(true);
            }
        });
    }
    
    private void cargarTabla()
    {
        GestorGrabarLeerDelDisco manejador=new GestorGrabarLeerDelDisco();
        LinkedList listaHerramientas=manejador.obtenerLista(new Operacion());
        String nombresColumnas[] = {"Operaciones"};
        operacionesTablaModel = new DefaultTableModel(nombresColumnas,0);
        Iterator iter = listaHerramientas.iterator();
        while(iter.hasNext())
        {
            Operacion aux = (Operacion)iter.next();
            String datosFila[] = {aux.getNombre()};
            operacionesTablaModel.addRow(datosFila);
            
        }
        tablaOperaciones.setModel(operacionesTablaModel);
    }
    
    private void cargarMouseListener()
    {
        MouseAdapter adaptadorMouse=new MouseAdapter()
           {@Override
           public void mouseClicked(MouseEvent e) 
              {
                 int fila = tablaOperaciones.rowAtPoint(e.getPoint());
                 int columna = tablaOperaciones.columnAtPoint(e.getPoint());
                 if ((fila > -1) && (columna > -1))
                 {
                     txtSeleccionada.setText(tablaOperaciones.getModel().getValueAt(fila, 0)+"");
//                     herramientaSeleccionada=tablaHerramientas.getModel().getValueAt(fila, 0)+"";
//                     pacienteSeleccionado.setDni(Integer.parseInt(tablaEstructuras.getModel().getValueAt(fila, 0)+""));
//                     pacienteSeleccionado.setApellidoNombre(tablaEstructuras.getModel().getValueAt(fila, 1)+"");
                 }
              }
           };
        
        tablaOperaciones.addMouseListener(adaptadorMouse);
    }
    
    DefaultTableModel operacionesTablaModel;
    private Operacion operacionSeleccionada;
    PantallaAgregarTrabajo pantallaPadre;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnNueva;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaOperaciones;
    private javax.swing.JTextField txtNuevaOperacion;
    private javax.swing.JTextField txtSeleccionada;
    // End of variables declaration//GEN-END:variables
}
